package org.springframework.samples.petclinic.common;

import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

/**
 * @author Chris
 * @date 2019/2/14
 * 全局异常处理类,使用SimpleMappingExceptionResolver 做全局异常处理
 * 优点：直接在一个方法里对需要处理的异常跳转不同的视图，比较简单方便
 * 缺点：无法把错误信息传递到视图层
 */

@Configuration
public class GlobalException {

    @Bean
    public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        // Properties properties = new Properties();
        /**
         * 参数一：异常的类型，注意必须是异常类型的全名
         * 参数二：视图名称
         */
        // properties.put("defaultErrorView", "exception");
        // properties.put("warnLogCategory", "warn");
        // properties.put("java.lang.NullPointerException", "error_nullPointer");
        // resolver.setExceptionMappings(properties);
        resolver.setDefaultErrorView("exception");
        resolver.setWarnLogCategory("warn");
        return resolver;
    }

}
